package com.qmhl.toolposition.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.github.gzuliyujiang.wheelpicker.DatimePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity
import com.hjq.toast.Toaster
import com.qmhl.toolposition.api.UserService
import com.qmhl.toolposition.base.BaseActivity
import com.qmhl.toolposition.databinding.ActivitySmoothBeforeBinding
import com.qmhl.toolposition.enu.CatSmooth
import com.qmhl.toolposition.utils.PUtils
import com.qmhl.toolposition.utils.PreferenceUtil
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date

class SmoothBeforeActivity : BaseActivity() {

    private lateinit var binding: ActivitySmoothBeforeBinding

    var sdf = SimpleDateFormat()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySmoothBeforeBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }


    override fun initData() {
        super.initData()
        sdf.applyPattern("yyyy-MM-dd")

        val date = Date()
        var format = sdf.format(date)

        binding.tvStartDate.text = format + " 00:00:00"
        binding.tvStopDate.text = format + " 23:59:59"


        var nickname = intent.getStringExtra("nickname")
        binding.tvMe.text = nickname

    }


    override fun initListener() {
        super.initListener()
        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.cat.setOnClickListener {
            isCanStart()
        }

        binding.llStartDate.setOnClickListener { onYearMonthDayTimeStart() }
        binding.llStopDate.setOnClickListener { onYearMonthDayTimeStop() }

    }

    private fun startMove() {
        var fromType = intent.getIntExtra("type", CatSmooth.ME.type)
        var friendId = intent.getIntExtra("friendId", 1)


        if (fromType == CatSmooth.FRIEND.type) {
            //查看朋友
            var intent = Intent(this, SmoothMoveActivity::class.java)
            intent.putExtra("type", CatSmooth.FRIEND.type)
            intent.putExtra("friendId", friendId)
            intent.putExtra("startTime", binding.tvStartDate.text.toString())
            intent.putExtra("endTime", binding.tvStopDate.text.toString())
            startActivity(intent)
            finish()
        } else {
            //查看自己
            var intent = Intent(this, SmoothMoveActivity::class.java)
            intent.putExtra("type", CatSmooth.ME.type)
            intent.putExtra("startTime", binding.tvStartDate.text.toString())
            intent.putExtra("endTime", binding.tvStopDate.text.toString())
            startActivity(intent)
            finish()
        }
    }

    private fun isCanStart() {

        lifecycleScope.launch {
            var content = UserService.create()
                .isVip()
            if (content.code == 200) {
                val isVip = content.data
                if (isVip) {
                    startMove()
                } else {
                    val intent = Intent(this@SmoothBeforeActivity, VipActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            } else {
                Toaster.showShort(content.msg)
            }
        }


    }

    fun onYearMonthDayTimeStart() {
        val picker = DatimePicker(this)
        val wheelLayout = picker.wheelLayout
        picker.setOnDatimePickedListener { year, month, day, hour, minute, second ->
            val text = "$year-$month-$day $hour:$minute:00"
            binding.tvStartDate.text = text
        }
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
        wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND)
        wheelLayout.setRange(DatimeEntity.dayOnFuture(-30), DatimeEntity.yearOnFuture(2))
        wheelLayout.setDefaultValue(DatimeEntity.now())
        wheelLayout.setDateLabel("年", "月", "日")
        wheelLayout.setTimeLabel("时", "分", "秒")
        picker.show()
    }


    fun onYearMonthDayTimeStop() {
        val picker = DatimePicker(this)
        val wheelLayout = picker.wheelLayout
        picker.setOnDatimePickedListener { year, month, day, hour, minute, second ->
            val text = "$year-$month-$day $hour:$minute:00"
            binding.tvStopDate.text = text
        }
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
        wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND)
        wheelLayout.setRange(DatimeEntity.dayOnFuture(-30), DatimeEntity.yearOnFuture(2))
        wheelLayout.setDefaultValue(DatimeEntity.now())
        wheelLayout.setDateLabel("年", "月", "日")
        wheelLayout.setTimeLabel("时", "分", "秒")
        picker.show()
    }


}